var orderInf;
apiready = function() {

	var orderNo = api.pageParam.orderNo;
	if (!orderNo) {
		api.closeWin();
		return;
	}
	var reqParam = JSON.stringify({
		orderno: orderNo
	});
	api.showProgress();
	$api.do_post('/api/pgroup/joins/data', {
		body: reqParam
	}, function(ret) {
		api.hideProgress();
		orderInf = ret[0];
		$api.getNetPicPath(orderInf.proimg, function(imgSrc) {
			$('#proImg').attr('src', imgSrc);
		});

		$('#proNames').append(orderInf.proNames);
		$('#orderGNum').text(orderInf.gNum + '人团');
		$('#price').text('价格：' + orderInf.price + '/' + orderInf.unit);

		if (orderInf.gNum != orderInf.gNumed) {
			$('#sealIcon').remove();
		}
		$api.getNetPicPath(orderInf.headimgurl, function(imgSrc) {
			$("#tuanMasterHead").attr("src", imgSrc||'images/temp/t1.jpg');
		});

		var gHtml = "";
		if (orderInf.gNum > orderInf.gNumed) {
			gHtml = '<p class="gm_tips">已参团' + orderInf.gNumed + '人</span>，还差<span>' + (orderInf.gNum - orderInf.gNumed) + '人</span>即可开团成功</p><div class="gm_surplus_time"><span><i class="time_icon"></i>剩余：<label id="clock"></label></span></div><ul style="display:none" class="gm_list"><li><b>参团</b><span>于2016-4-30  17:00</span><strong>邀请好友</strong><i><img src="../../image/temp/t1.jpg" /></i></li></ul>';
		}
		$("#groupMember").append(gHtml);
		var list = orderInf.rows;
		if (list) {
			$.each(list, function(i, item) {

				var html = $('#tuanT').html();


				for (var key in item) {
					var regex = new RegExp('{{' + key + '}}', 'g');
					html = html.replace(regex, item[key]);
				}


				$('#tuans').append(html);
			});
		}

		if (orderInf.gNum > orderInf.gNumed) {
			displayTime();
		}

		if (new Date(orderInf.eDate) > new Date()) {
			$("#shareGroup").remove();
		}

		if (orderInf.gNum > orderInf.gNumed) {
			$('#groupAlert').remove();
		}
	});
};

$('.groupInfoBtn').each(function() {
	this.on('click', function() {
		$api.mOpenWin('groupInfo', 'group_info.html', {
			id: orderInf.proids
		});
	});
});

$('.openGroupWin').on('click', function() {
	openGroupWin(orderInf.proids);
})

function openGroupWin(groupId) {
	$api.mOpenWin('groupInf', 'group_info.html', {
		id: groupId
	});
}

function alert() {
	if (orderInf.status == "待成团") {
		var wxAppId = 'wx08e69fae39607f0f';
		var redirect = encodeURIComponent("http://comm.72gs.net/wx/pgroup/order/join/" + orderInf.orderNo);
		var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+wxAppId+"&redirect_uri=" + redirect + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
		var sharedModule = api.require('shareAction');
		sharedModule.share({
			text: url,
			type: 'text'
		});
	}else{
		$api.toastMsg("拼团已结束");
	}

	// api.confirm({
	// 	title: '分享',
	// 	msg: "复制分享",
	// 	buttons: ['取消', '点我复制']
	// }, function(ret, err) {
	// 	if (ret.buttonIndex == '2') {
	// 		var clipBoard = api.require('clipBoard');
	// 		clipBoard.set({
	// 			value: url
	// 		}, function(ret, err) {
	// 			if (ret.status) {
	// 				$api.toastMsg("复制成功");
	// 			}
	// 		});
	// 	}
	// });
	// var wx = api.require('wx');
	// wx.shareWebpage({
	// 	apiKey: 'wxddf344b4f1841887',
	// 	scene: 'timeline',
	// 	title: '参团分享',
	// 	description: orderInf.proNames,
	// 	contentUrl: 'http://comm.72gs.net/pgroup/order/join/' + orderInf.orderNo
	// }, function(ret, err) {
	// 	if (ret.status) {
	// 		alert('分享成功');
	// 	} else {
	// 		alert(err.code);
	// 	}
	// });

	// $(".group_alert").show();
}

// wx.config({
// 	debug: false,
// 	//appId: 'wx08e69fae39607f0f',
// 	appId: '<%=wx.appId %>',
// 	timestamp: '<%=wx.timestamp %>',
// 	nonceStr: '<%=wx.nonceStr %>',
// 	signature: '<%=wx.signature %>',
// 	jsApiList: ['onMenuShareTimeline',
// 		'onMenuShareAppMessage',
// 		'onMenuShareQQ',
// 		'onMenuShareWeibo',
// 		'onMenuShareQZone',
// 	]
// });

// wx.ready(function() {

// 	var redirect = encodeURI("http://demo.72gs.net/wx/pgroup/order/join/<%=orderinfo.orderNo||''%>");
// 	var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=<%=wx.appId %>&redirect_uri=" + redirect + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
// 	var imgurl = 'http://demo.72gs.net/files/<%=orderinfo.proimg||""%>';

// 	wx.onMenuShareAppMessage({
// 		title: '<%=proinfo.name||""%>', // 分享标题
// 		desc: '<%=proinfo.sglang||""%>', // 分享描述
// 		link: url, // 分享链接
// 		imgUrl: imgurl, // 分享图标
// 		type: 'link', // 分享类型,music、video或link，不填默认为link
// 		dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
// 		success: function() {
// 			// 用户确认分享后执行的回调函数
// 			//alert('ok')
// 		},
// 		cancel: function() {
// 			// 用户取消分享后执行的回调函数
// 			//alert('no')
// 		},
// 		fail: function(res) {
// 			alert(JSON.stringify(res));
// 		}
// 	});

// 	wx.onMenuShareTimeline({
// 		title: '<%=proinfo.name||""%>', // 分享标题
// 		link: url, // 分享链接
// 		imgUrl: imgurl, // 分享图标
// 		success: function() {
// 			// 用户确认分享后执行的回调函数
// 		},
// 		cancel: function() {
// 			// 用户取消分享后执行的回调函数
// 		},
// 		fail: function(res) {
// 			alert(JSON.stringify(res));
// 		}
// 	});


$('#btnShare').click(function() {

	return false;
});

// wx.error(function(res) {
// 	alert(JSON.stringify(res));
// });

function displayTime() {
	$api.do_get("/api/products/info?id=" + orderInf.proids, function(ret) {

		var elt = document.getElementById("clock");

		var endTime = new Date(ret.eDate.replace(/-/g, '/'));
		//$("#tipmessage").html(endTime);
		var now = new Date();
		var leftTime = endTime.getTime() - now.getTime();

		if (leftTime < 0) {
			elt.innerHTML = "00:00:00";
		} else {
			var ms = parseInt(leftTime % 1000).toString();
			leftTime = parseInt(leftTime / 1000);
			var o = Math.floor(leftTime / 3600);
			var d = Math.floor(o / 24);
			var m = Math.floor(leftTime / 60 % 60);
			var s = leftTime % 60;
			elt.innerHTML = o + ":" + m + ":" + s + ":" + ms.charAt(0);
			setTimeout(displayTime, 100);
		}

	});



}